<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>函数 | 老李的个人博客</title>
    <meta name="description" content="老李的个人网站，记录着自己的个人成长。">
    <link rel="icon" href="/logo.jpg">
    
    <link rel="preload" href="/assets/css/0.styles.1db6e86b.css" as="style"><link rel="preload" href="/assets/js/app.9a5f4f15.js" as="script"><link rel="preload" href="/assets/js/2.fb839ad7.js" as="script"><link rel="preload" href="/assets/js/9.548be40e.js" as="script"><link rel="prefetch" href="/assets/js/10.3edf41db.js"><link rel="prefetch" href="/assets/js/11.7ba525f6.js"><link rel="prefetch" href="/assets/js/12.bf9d74b3.js"><link rel="prefetch" href="/assets/js/13.b1401722.js"><link rel="prefetch" href="/assets/js/14.35ef07af.js"><link rel="prefetch" href="/assets/js/15.ffbfdd10.js"><link rel="prefetch" href="/assets/js/3.ce5bc181.js"><link rel="prefetch" href="/assets/js/4.76416af2.js"><link rel="prefetch" href="/assets/js/5.1b1ec201.js"><link rel="prefetch" href="/assets/js/6.529193cc.js"><link rel="prefetch" href="/assets/js/7.efd2306a.js"><link rel="prefetch" href="/assets/js/8.92fb4af1.js">
    <link rel="stylesheet" href="/assets/css/0.styles.1db6e86b.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">老李的个人博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">算法学习网站</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://leetcode-cn.com/problemset/all/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  力扣
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="https://www.runoob.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  菜鸟教程
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">Python</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/Django/" class="nav-link">Django</a></li><li class="dropdown-item"><!----> <a href="/blog/HTML/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/Python/" class="nav-link router-link-active">Pthon</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">算法学习网站</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://leetcode-cn.com/problemset/all/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  力扣
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="https://www.runoob.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  菜鸟教程
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">Python</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/Django/" class="nav-link">Django</a></li><li class="dropdown-item"><!----> <a href="/blog/HTML/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/Python/" class="nav-link router-link-active">Pthon</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>函数</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/Python/%E5%87%BD%E6%95%B0/#函数" class="sidebar-link">函数</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/Python/%E5%87%BD%E6%95%B0/#关键字参数" class="sidebar-link">关键字参数</a></li><li class="sidebar-sub-header"><a href="/blog/Python/%E5%87%BD%E6%95%B0/#可变参数列表" class="sidebar-link">可变参数列表</a></li><li class="sidebar-sub-header"><a href="/blog/Python/%E5%87%BD%E6%95%B0/#可变关键字对象" class="sidebar-link">可变关键字对象</a></li></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h2 id="函数"><a href="#函数" aria-hidden="true" class="header-anchor">#</a> 函数</h2> <h3 id="关键字参数"><a href="#关键字参数" aria-hidden="true" class="header-anchor">#</a> 关键字参数</h3> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">parrot</span><span class="token punctuation">(</span>voltage<span class="token punctuation">,</span> state<span class="token operator">=</span><span class="token string">'a stiff'</span><span class="token punctuation">,</span> action<span class="token operator">=</span><span class="token string">'voom'</span><span class="token punctuation">,</span> <span class="token builtin">type</span><span class="token operator">=</span><span class="token string">'Norwegian Blue'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;-- This parrot wouldn't&quot;</span><span class="token punctuation">,</span> action<span class="token punctuation">,</span> end<span class="token operator">=</span><span class="token string">' '</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;if you put&quot;</span><span class="token punctuation">,</span> voltage<span class="token punctuation">,</span> <span class="token string">&quot;volts through it.&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;-- Lovely plumage, the&quot;</span><span class="token punctuation">,</span> <span class="token builtin">type</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;-- It's&quot;</span><span class="token punctuation">,</span> state<span class="token punctuation">,</span> <span class="token string">&quot;!&quot;</span><span class="token punctuation">)</span>
</code></pre></div><p>可以以下几种方式被调用:</p> <div class="language-python extra-class"><pre class="language-python"><code>parrot<span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span>                                          <span class="token comment"># 1 positional argument</span>
parrot<span class="token punctuation">(</span>voltage<span class="token operator">=</span><span class="token number">1000</span><span class="token punctuation">)</span>                                  <span class="token comment"># 1 keyword argument</span>
parrot<span class="token punctuation">(</span>voltage<span class="token operator">=</span><span class="token number">1000000</span><span class="token punctuation">,</span> action<span class="token operator">=</span><span class="token string">'VOOOOOM'</span><span class="token punctuation">)</span>             <span class="token comment"># 2 keyword arguments</span>
parrot<span class="token punctuation">(</span>action<span class="token operator">=</span><span class="token string">'VOOOOOM'</span><span class="token punctuation">,</span> voltage<span class="token operator">=</span><span class="token number">1000000</span><span class="token punctuation">)</span>             <span class="token comment"># 2 keyword arguments</span>
parrot<span class="token punctuation">(</span><span class="token string">'a million'</span><span class="token punctuation">,</span> <span class="token string">'bereft of life'</span><span class="token punctuation">,</span> <span class="token string">'jump'</span><span class="token punctuation">)</span>         <span class="token comment"># 3 positional arguments</span>
parrot<span class="token punctuation">(</span><span class="token string">'a thousand'</span><span class="token punctuation">,</span> state<span class="token operator">=</span><span class="token string">'pushing up the daisies'</span><span class="token punctuation">)</span>  <span class="token comment"># 1 positional, 1 keyword</span>
</code></pre></div><p>错误调用方法：</p> <div class="language-python extra-class"><pre class="language-python"><code>parrot<span class="token punctuation">(</span><span class="token punctuation">)</span>                     <span class="token comment"># required argument missing</span>
parrot<span class="token punctuation">(</span>voltage<span class="token operator">=</span><span class="token number">5.0</span><span class="token punctuation">,</span> <span class="token string">'dead'</span><span class="token punctuation">)</span>  <span class="token comment"># non-keyword argument after a keyword argument</span>
parrot<span class="token punctuation">(</span><span class="token number">110</span><span class="token punctuation">,</span> voltage<span class="token operator">=</span><span class="token number">220</span><span class="token punctuation">)</span>     <span class="token comment"># duplicate value for the same argument</span>
parrot<span class="token punctuation">(</span>actor<span class="token operator">=</span><span class="token string">'John Cleese'</span><span class="token punctuation">)</span>  <span class="token comment"># unknown keyword argument</span>
</code></pre></div><h3 id="可变参数列表"><a href="#可变参数列表" aria-hidden="true" class="header-anchor">#</a> 可变参数列表</h3> <p>可以让函数调用可变个数的参数.这些参数被包装进一个元组(查看元组和序列).在这些可变个数的参数之前,可以有零到多个普通的参数(*args 一个*号表示):</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">arithmetic_mean</span><span class="token punctuation">(</span><span class="token operator">*</span>args<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token builtin">sum</span> <span class="token operator">=</span> <span class="token number">0</span>
    <span class="token keyword">for</span> x <span class="token keyword">in</span> args<span class="token punctuation">:</span>
        <span class="token builtin">sum</span> <span class="token operator">+=</span> x
    <span class="token keyword">return</span> <span class="token builtin">sum</span>


<span class="token keyword">print</span><span class="token punctuation">(</span>arithmetic_mean<span class="token punctuation">(</span><span class="token number">45</span><span class="token punctuation">,</span> <span class="token number">32</span><span class="token punctuation">,</span> <span class="token number">89</span><span class="token punctuation">,</span> <span class="token number">78</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>arithmetic_mean<span class="token punctuation">(</span><span class="token number">8989.8</span><span class="token punctuation">,</span> <span class="token number">78787.78</span><span class="token punctuation">,</span> <span class="token number">3453</span><span class="token punctuation">,</span> <span class="token number">78778.73</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>arithmetic_mean<span class="token punctuation">(</span><span class="token number">45</span><span class="token punctuation">,</span> <span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>arithmetic_mean<span class="token punctuation">(</span><span class="token number">45</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>arithmetic_mean<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><p>结果：</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token number">244</span>
<span class="token number">170009.31</span>
<span class="token number">77</span>
<span class="token number">45</span>
<span class="token number">0</span>
</code></pre></div><h3 id="可变关键字对象"><a href="#可变关键字对象" aria-hidden="true" class="header-anchor">#</a> 可变关键字对象</h3> <p>可以让函数调用可变个数的参数.这些参数被包装进一个<code>字典</code>.在这些可变个数的参数之前,可以有零到多个普通的参数和(**kwarg <code>keyword arguments</code>关键参数):</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">test_args</span><span class="token punctuation">(</span><span class="token operator">*</span>args<span class="token punctuation">,</span> <span class="token operator">**</span>kwargs<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>kwargs<span class="token punctuation">)</span>
</code></pre></div><p>结果：</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span><span class="token string">'name'</span><span class="token punctuation">:</span> <span class="token string">'master'</span><span class="token punctuation">,</span> <span class="token string">'user'</span><span class="token punctuation">:</span> <span class="token string">'user'</span><span class="token punctuation">,</span> <span class="token string">'day'</span><span class="token punctuation">:</span> <span class="token string">'6'</span><span class="token punctuation">}</span>
</code></pre></div></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.9a5f4f15.js" defer></script><script src="/assets/js/2.fb839ad7.js" defer></script><script src="/assets/js/9.548be40e.js" defer></script>
  </body>
</html>
